﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace WordMine
{
    class Unit : GameObject
    {
        public int x;
        public int y;
        public Rectangle sourceRect;
        public int totalFrames;
        public int rows;
        public int columns;
        public int imageCode;
        public int milliseconds;
        public int attackspeed;
        public Boolean finished;
        public Boolean goingUp;
        public int currentFrame;
        public Symbol symbol;
        public int positionChange;
        public int hp;
        public int ap;

        public Unit(int health, int attack, Vector2 position)
            : base("foreground/unit", position)
        {
            imageCode = 2;
            scale = (float)health / 2;
            hp = health*10;
            ap = attack;
            this.symbol = Symbol.none;
            if (position.X > 500)
            {
                positionChange = -2;
                imageCode = 1;
            }
            else
                positionChange = 2;
            this.totalFrames = 9;
            this.columns = 3;
            this.rows = 4;
            this.milliseconds = 0;
            attackspeed = 0;
            currentFrame = 0;
            this.noAnimate = true;

            this.finished = true;
        }

        public override void LoadContent(ContentManager content)
        {
            base.LoadContent(content);
            this.position.Y -= 16 * scale-16;
            this.sourceRect = new Rectangle(0, this.texture.Height / this.rows*imageCode, this.texture.Width / this.columns, this.texture.Height / this.rows);
            Rectangle modifiedSourceRect = sourceRect;
            modifiedSourceRect.Width = (int)(modifiedSourceRect.Width *scale);
            modifiedSourceRect.Height = (int)(modifiedSourceRect.Height * scale);
            this.rectangle.X = (int)this.position.X - modifiedSourceRect.Width / 2;
            this.rectangle.Y = (int)this.position.Y - modifiedSourceRect.Height / 2;
            this.rectangle.Width = modifiedSourceRect.Width;
            this.rectangle.Height = modifiedSourceRect.Height;
        }
        public void checkEnemy(Unit unit)
        {
            if (unit.rectangle.Intersects(rectangle))
            {
                milliseconds = 2;
                if(attackspeed--<=0)
                    swingSword(unit);

            }
        }
        public void swingSword(Unit unit)
        {
            attackspeed = 100;
            unit.hp -= ap;
            hp -= unit.ap;
        }
        public void checkFriendly(Unit unit)
        {
            if (unit.rectangle.Intersects(rectangle))
                milliseconds = 2;
        }
        public override void Update(GameTime gameTime)
        {
                milliseconds--;
                if (milliseconds < 0)
                {
                    milliseconds = 10;
                    if (goingUp)
                        currentFrame++;
                    if (!goingUp)
                        currentFrame--;
                    if (currentFrame >= 2)
                        goingUp = false;
                    if (currentFrame <= 0)
                        goingUp = true;

                    base.Update(gameTime);
                    this.sourceRect = new Rectangle(currentFrame * this.texture.Width / this.columns, this.texture.Height / this.rows * imageCode, this.texture.Width / this.columns, this.texture.Height / this.rows);
                    this.zindex = (float)(0.3 + ((1.2 - (((float)this.y / (float)10) + .3f)) / (float)10));
                    position.X += positionChange;

                    Rectangle modifiedSourceRect = sourceRect;
                    modifiedSourceRect.Width = (int)(modifiedSourceRect.Width*scale);
                        modifiedSourceRect.Height = (int)(modifiedSourceRect.Height*scale);
                        this.rectangle.X = (int)this.position.X - modifiedSourceRect.Width / 2;
                        this.rectangle.Y = (int)this.position.Y - modifiedSourceRect.Height / 2;
                }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (this.visible)
            {
                if (this.texture != null)
                {
                    spriteBatch.Draw(this.texture, this.position, this.sourceRect, this.tintColor, this.rotation, new Vector2(sourceRect.Width / 2, sourceRect.Height / 2), scale, SpriteEffects.None, this.zindex);
                }
            }
        }
    }
}
